package me.pplay.playtalk.adapter;

import java.util.List;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.bean.Chat;
import me.pplay.playtalk.database.FriendDb;
import me.pplay.playtalk.database.NotificationDb;
import me.pplay.playtalk.holder.MessageHolder;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.util.AsyncImageLoader;
import me.pplay.playtalk.util.AsyncImageLoader.ImageCallback;
import me.pplay.playtalk.util.DateParseUtil;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MessageAdapter extends BaseAdapter {
	private Context mContext;
	private List<Chat> mList;
    private AsyncImageLoader imageLoader;
    private TpChat tpChat;
	
	public MessageAdapter(Context context, List<Chat> mList, AsyncImageLoader imageLoader){
		mContext			= context;
		this.mList			= mList;
		this.imageLoader	= imageLoader;
		tpChat				= new ApplicationTrans().getTpChat();
	}
	
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MessageHolder holder = null;
		if(convertView == null){
			holder				= new MessageHolder();
			convertView			= (View)LayoutInflater.from(mContext).inflate(R.layout.message_item, null);
			holder.icon			= (ImageView)convertView.findViewById(R.id.icon);
			holder.title		= (TextView)convertView.findViewById(R.id.title);
			holder.des			= (TextView)convertView.findViewById(R.id.des);
			holder.timeTv		= (TextView)convertView.findViewById(R.id.timeTv);
			holder.img			= (ImageView)convertView.findViewById(R.id.img);
			holder.layout_count	= (RelativeLayout)convertView.findViewById(R.id.layout_count);
			holder.countTv		= (TextView)convertView.findViewById(R.id.countTv);
			convertView.setTag(holder);
		}else{
			holder 		= (MessageHolder)convertView.getTag();
		}
		Chat chat		= mList.get(position);
		if(position == 0 && chat == null){	//通知
			holder.icon.setBackgroundResource(R.drawable.home_notification_icon_selector);
			holder.title.setText(mContext.getString(R.string.notifications_service));
			holder.img.setVisibility(View.GONE);
			holder.des.setVisibility(View.GONE);
			holder.timeTv.setText("");
			holder.layout_count.setVisibility(View.VISIBLE);
			NotificationDb dbNoti 	= new NotificationDb(mContext);
			FriendDb dbFriend		= new FriendDb(mContext);
			int count 	= dbNoti.queryList(0).size() + dbFriend.queryList(2).size();
			holder.countTv.setText(count + "");
		}else if(chat != null){
			if(chat.getType() == 0){
				setImageIcon(holder.icon, chat.getSkyid());
			}else if(chat.getType() == 5){
				holder.icon.setBackgroundResource(R.drawable.icon_group_default);
			}
			holder.title.setText(chat.getName());
			holder.des.setText(chat.getMessage());
			if(chat.getTime() != null && !chat.getTime().equals(""))
				holder.timeTv.setText(DateParseUtil.getTimeText(Long.parseLong(chat.getTime())));
			else
				holder.timeTv.setText("");
			if(chat.getCount() > 0)
				holder.img.setVisibility(View.VISIBLE);
			else
				holder.img.setVisibility(View.INVISIBLE);
			holder.des.setVisibility(View.VISIBLE);
			holder.layout_count.setVisibility(View.GONE);
			
		}else{
			return null;
		}
		return convertView;
	}
	
	private void setImageIcon(final ImageView imageView, final String imageUrl){
		imageView.setBackgroundResource(R.drawable.ic_launcher);
		Bitmap bitmap = imageLoader.loadBitmap(imageUrl, new ImageCallback() {
			
			@Override
			public void imageLoaded(Bitmap imageBitmap, String imageUrl) {
				if(imageBitmap != null)
					imageView.setImageBitmap(imageBitmap);
			}
		});
		if(bitmap != null)
			imageView.setImageBitmap(bitmap);
	}

	
	private void setImageIcon(final ImageView imageView, final int skyid){
		TPBuddyinfo buddyinfo 	= tpChat.tp_chat_getBuddyinfo(skyid);
		try {
			if(buddyinfo == null){
				imageView.setBackgroundResource(R.drawable.class.getDeclaredField("avatar0").getInt(mContext));
			}else{
				int avatar_sid 	= buddyinfo.getAvatar_sid();
				if(buddyinfo.getAvatar_fpath().equals("")){
					String name 	= "";
					if(avatar_sid == 0){
						name = buddyinfo.getSex() == 1?"avatar0":"avatarf";
					}else{
						name = "avatar"+avatar_sid;
					}
					imageView.setBackgroundResource(R.drawable.class.getDeclaredField(name).getInt(mContext));
				}else{
					setImageIcon(imageView, "http://" + tpChat.tp_chat_gethttpaddr_data(avatar_sid) + "/" + buddyinfo.getAvatar_fpath());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
